var lastRoot;

var lastComment;

function loadComment(commentId, rootId) {
	clearExplodedComments(rootId);
	var comment = $("#"+commentId);
	var pvw = comment.children("div.preview:first");
	var exp = comment.children("div.exploded:first");
	pvw.hide();
	exp.load(comment_load_url, {'id' : commentId, 'loadChildren': false});
	exp.show();
	lastRoot = rootId;
	lastComment = commentId;
	return false;
}
function clearExplodedComments(rootId) {
	clearReplyForms(rootId);
	//var expd = root.children(".exploded");
	var expd = $("#"+rootId+" .exploded");
	expd.hide();
	var pvw = $("#"+rootId+" .preview");
	pvw.show();
}
function reloadThread(rootId) {
	$("#"+rootId).load(comment_load_url+"?id="+rootId);
	return false;
}
function tag(commentId, tags) {
	var taglist = $("#"+commentId+" div.commentTags:first");
	taglist.load(comment_tag_url, {'commentId' : commentId, 'tags': tags});
	return false;
}
function keytag(evt, commentId, tags) {
	 var key = (evt) ? evt.which : event.keyCode;
	 if(key == 13) {
		 return tag(commentId, tags);
	 }
	 return false;
}
function toggleAddTag(commentId) {
	$("#"+commentId+" span.addTagLink:first").toggle();
	$("#"+commentId+" input.addTag:first").toggle().focus();
	return false;
}

function reply(commentId) {
	var clonedForm = $("#replyForm").clone().addClass("reply-clone");
	var title = "Post a Comment";
	if(commentId) {
		title = "Post a Reply";
	}
	clonedForm.find("#parentId").attr("value", commentId)
	clonedForm.dialog({
		draggable : true, 
		title: title,
		width: 415,
		close: function(event, ui) {$(".reply-clone").remove();}
	});
	return false;
}
function clearReplyForms(rootId) {
	$("#"+rootId+" .replyForm").remove();
}